package com.yyydjk.library;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


/**
 * Created by dongjunkun on 2015/6/17.
 */
public class DropDownMenu extends LinearLayout implements TabClickListener {
    /**
     * 底部容器，包含popupMenuViews，maskView
     */
    private FrameLayout containerView;

    /**
     * 遮罩半透明View，点击可关闭DropDownMenu
     */
    private View maskView;
    /**
     * 遮罩颜色
     */
    private int maskColor = 0x88888888;
    private MenuTabDelegate mTabDelegate;
    private MenuDropWindowDelegate mDropWindowDelegate;


    public DropDownMenu (Context context) {
        super(context, null);
    }

    public DropDownMenu (Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DropDownMenu (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init (Context context, AttributeSet attrs) {
        mTabDelegate = new MenuTabDelegate();
        mDropWindowDelegate = new MenuDropWindowDelegate();
        setOrientation(VERTICAL);

        //为DropDownMenu添加自定义属性
        int menuBackgroundColor = 0xffffffff;
        int underlineColor = 0xffcccccc;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DropDownMenu);
        underlineColor = a.getColor(R.styleable.DropDownMenu_ddunderlineColor, underlineColor);
        int dividerColor = a.getColor(R.styleable.DropDownMenu_dddividerColor, mTabDelegate.getDividerColor());
        menuBackgroundColor = a.getColor(R.styleable.DropDownMenu_ddmenuBackgroundColor, menuBackgroundColor);
        maskColor = a.getColor(R.styleable.DropDownMenu_ddmaskColor, maskColor);
        float menuHeightPercent = a.getFloat(R.styleable.DropDownMenu_ddmenuMenuHeightPercent, mDropWindowDelegate.getMenuHeightPercent());
        mDropWindowDelegate.setMenuHeightPercent(menuHeightPercent);
        a.recycle();

        final View menu = mTabDelegate.initMenu(context, menuBackgroundColor);
        addView(menu, 0);
        mTabDelegate.setDividerColor(dividerColor);
        mTabDelegate.addTabClickListener(this);

        //为tabMenuView添加下划线
        View underLine = new View(getContext());
        underLine.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DeviceUtils.dpTpPx(1.0f, getContext())));
        underLine.setBackgroundColor(underlineColor);
        addView(underLine, 1);

        //初始化containerView并将其添加到DropDownMenu
        containerView = new FrameLayout(context);
        containerView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        addView(containerView, 2);

    }

    /**
     * 初始化DropDownMenu
     */
    public <T> void setDropDownMenu (LinkedHashMap<T, View> tabViewMap, Class... classes) {
        final List<T> tabTexts = new ArrayList<>(tabViewMap.keySet());
        final List<View> popupViews = new ArrayList<>(tabViewMap.values());
        mTabDelegate.setTabData(tabTexts);
        mTabDelegate.setTabClassList(classes);
        mDropWindowDelegate.setDropViews(popupViews);
        mTabDelegate.addTabViews(getContext());
        mDropWindowDelegate.addDropViews(getContext());
    }

    public void bindingContentView (@NonNull View contentView) {
        containerView.addView(contentView, 0);

        maskView = new View(getContext());
        maskView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        maskView.setBackgroundColor(maskColor);
        maskView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick (View v) {
                closeMenu();
            }
        });
        containerView.addView(maskView, 1);
        maskView.setVisibility(GONE);
        if (containerView.getChildAt(2) != null) {
            containerView.removeViewAt(2);
        }

        mDropWindowDelegate.initDropViewParent(getContext());
        containerView.addView(mDropWindowDelegate.getDropViewParent(), 2);
    }

    /**
     * 改变指定位置的tab文字
     *
     * @param data
     */
    public <T> void setTabText (int index, T data) {
        mTabDelegate.setTabText(index, data);
    }

    /**
     * 改变点击位置的tab文字
     *
     * @param data
     * @param <T>
     */
    public <T> void setTabText (T data) {
        mTabDelegate.setTabText(-1, data);
    }


    /**
     * 关闭菜单
     */
    public void closeMenu () {
        if (mTabDelegate.getClickIndex() != -1) {
            mTabDelegate.closeMenu();
            mDropWindowDelegate.closeMenu();
            maskView.setVisibility(GONE);
            maskView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.dd_mask_out));
        }
    }

    public void addTabClickListener (TabClickListener tabClickListener) {
        mTabDelegate.addTabClickListener(tabClickListener);
    }

    /**
     * DropDownMenu是否处于可见状态
     *
     * @return
     */
    public boolean isShowing () {
        return mTabDelegate.getClickIndex() != -1;
    }
    public void openTab(int position){
        onTabClick(position,null);
    }
    @Override
    public void onTabClick (int position, ITabView view) {
        if (mTabDelegate.getClickIndex() == -1) {
            final boolean showDrop = mDropWindowDelegate.showDrop(position);
            if (!showDrop) {
                return;
            }
            maskView.setVisibility(VISIBLE);
            maskView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.dd_mask_in));
            mTabDelegate.setClickIndex(position);
            return;
        }
        if (mTabDelegate.getClickIndex() == position) {
            closeMenu();
            return;
        }
        final boolean showDrop = mDropWindowDelegate.showDrop(position);
        if (!showDrop) {
            return;
        }
        mTabDelegate.setClickIndex(position);
    }
}
